package GUI.Lesson;

import java.awt.*;
import java.awt.event.*;

public class TextCalculator {
    public static void main(String[] args) {
        new Calculator();
    }
}

class Calculator extends Frame {
    public Calculator() {
        //三个文本框
        TextField textField = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(20);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new Action(textField,textField2,textField3));

        //一个标签
        Label label = new Label("+");

        //布局
        setLayout(new FlowLayout());

        add(textField);
        add(label);
        add(textField2);
        add(button);
        add(textField3);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        pack();
        setVisible(true);
    }
}

//监听类
class Action implements ActionListener {
    private TextField num1,num2,num3;
    public Action(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        //2.将这个值加法运算后赋值给第三个文本框
        num3.setText(""+(n1+n2));

        //清楚前两个文本框
        num1.setText("");
        num2.setText("");
    }
}
